##This code creates the ideal point graphs from the estimates.

library(ggplot2)
library(dplyr)
library(readr)
library(countrycode)

df <- read_csv("Idealpointestimates.csv")


df$Lastname <- paste(toupper(substr(df$lastname,1,1)), ## Just capitalizing the names. 
                     substr(df$lastname,2,nchar(df$lastname)), sep = "")

df$Lastname <- ifelse(df$lastname == "Stadlmayer", 
                      "Kucsko-Stadlmayer",
                      ifelse(df$Lastname == "Trajkovska", 
                             "Lazarova Trajkovska",
                             ifelse(df$Lastname == "Rocha",
                                    "Lopes Rocha", 
                                    ifelse(df$Lastname == "Robert", 
                                           "Bindschedler-Robert", 
                                           ifelse(df$Lastname == "Ridruejo",
                                                  "Pastor Ridruejo",
                                                  ifelse(df$Lastname == "Pallieri",
                                                         "Balladore Pallieri",
                                                         ifelse(df$Lastname == "Nikolovska", 
                                                                "Tsatsa-Nikolovska",
                                                                ifelse(df$Lastname== "Albuquerque",
                                                                       "Pinto de Albuquerque",
                                                                       ifelse(df$Lastname == "Power", 
                                                                              "Power-Forde", 
                                                                              ifelse(df$Lastname == "Gaetano",
                                                                                     "de Gaetano",
                                                                                     ifelse(df$Lastname == "Bravo",
                                                                                            "Ferrari Bravo", 
                                                                                            ifelse(df$Lastname == "Barreto", 
                                                                                                   "Cabral Barreto",
                                                                                                   ifelse(df$Lastname == "SpielmannD",
                                                                                                          "Spielmann, Dean",
                                                                                                          ifelse(df$Lastname == "KurisE",
                                                                                                                 "Kuris, Egidijus",
                                                                                                                 df$Lastname))))))))))))))


idealPoints <- df %>% 
  mutate(cowcode = ifelse(country == "Serbia", 345, countrycode(country, "country.name", "cown")),
         restraint = Mean,
         lower = restraint - 1.96*abs(SD),
         upper = restraint + 1.96*abs(SD)) 

idealPoints$Appointment <- ifelse(idealPoints$appointmentyear > 2009, "2010-2016", 
                                  ifelse(idealPoints$appointmentyear > 2005, "2005-2009",
                                         ifelse(idealPoints$appointmentyear> 1997, "1998-2004", 
                                                NA)))


################# Figure 1 ############################
pdf("Figure1.pdf", 
    width = 6.5, height = 6.5)
print(ggplot(subset(idealPoints,appointmentyear>1997 & is.na(restraint) == FALSE), 
             aes(x=restraint, y= reorder(Lastname,restraint),xmin=lower, xmax=upper, color = Appointment, 
                 shape = Appointment)) +  
        geom_point()+
        geom_errorbarh() + 
        theme_classic()+
        ylab("") + 
        geom_vline(xintercept = -3:3,linetype=3)+
        theme(axis.text=element_text(size=7))+
        xlab("Judicial restraint"))
dev.off()

################ Figure 2 ###############################
pdf("Figure2.pdf",width = 6.5, height = 6.5)
print(ggplot(subset(idealPoints,appointmentyear>1997),  aes(x=appointmentyear, y=restraint ))+
        geom_point()+ theme_classic()+geom_smooth(method = "loess") +
        labs(x="Appointment Year",y="Judicial Restraint") +
        scale_x_continuous(breaks=seq(1990, 2016, 5))) 
dev.off()


